package com.ray.activiti.controller;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ray.activiti.common.validate.ValidatorUtils;
import com.ray.activiti.enums.BusType;
import com.ray.activiti.service.ExtendActBusinessService;
import com.ray.activiti.table.entity.ExtendActBusiness;
import com.ray.activiti.util.DataGridUtil;
import com.ray.api.service.UcenterApiService;
import com.ray.auth.system.table.entity.Button;
import com.ray.auth.system.table.entity.Organization;
import com.xiaoleilu.hutool.lang.Assert;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.web.controller.WebController;

/**
 * 类的功能描述.
 * 业务树
 * @Auther ray  qray686898@163.com
 * @Date 2017/12/28
 * @代码参考或来自  https://github.com/huangxianyuan/hxyFrame
 */
@RequestMapping("act/bus")
@Controller
public class ExtendActBusinessController extends WebController{

    @Autowired
    ExtendActBusinessService businessService;
    @Autowired
    UcenterApiService  ucenterApiService;

    @RequestMapping("view")
    @RequiresPermissions("act:bus:view")
    public ModelAndView view(ModelAndView modelAndView ,String menuId){
    	LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
	  	modelAndView.setViewName("activiti/bus/view");
       return  modelAndView;
    }
    


   
  
    
    @SuppressWarnings("unchecked")
	@RequestMapping("processQuery")
    @RequiresPermissions("act:bus:processQuery")
    @ResponseBody
    public ResponseEntity<Map<String,Object>> query(ModelAndView modelAndView , ExtendActBusiness businessEntity){
    	LoginUser user = getLoginUser();
    	businessEntity.setType(BusType.PROCESS.getType());
        List<ExtendActBusiness> businessEntityList = businessService.queryListByBean(businessEntity,user);
        PageDto<ExtendActBusiness> page= this.createJQPage(request);
        page.setRecords(businessEntityList);
		return doSuccess(DataGridUtil.dataConvert(page));
    }
    
    @SuppressWarnings("unchecked")
	@RequestMapping("processQueryWindow")
    @ResponseBody
    public ResponseEntity<Map<String,Object>> processQueryWindow(ModelAndView modelAndView , ExtendActBusiness businessEntity){
    	LoginUser user = getLoginUser();
    	businessEntity.setType(BusType.PROCESS.getType());
        List<ExtendActBusiness> businessEntityList = businessService.queryListByBean(businessEntity,user);
        PageDto<ExtendActBusiness> page= this.createJQPage(request);
        page.setRecords(businessEntityList);
		return doSuccess(DataGridUtil.dataConvert(page));
    }

    
    
    @RequestMapping("processAddView")
    @RequiresPermissions("act:bus:processAdd")
    public ModelAndView addView(ModelAndView modelAndView ,String menuId,String parentId,String parentName){
    	LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
	  
	  	modelAndView.addObject("parentId",parentId);
	  	modelAndView.addObject("parentName",parentName);
	  	modelAndView.setViewName("activiti/bus/process/add");
       return  modelAndView;
    }
    
    /**
     * 保存或者更新
     * @param model
     * @param businessEntity
     * @return
     */
    @RequestMapping(value = "processAdd",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("act:bus:processAdd")
    public ResponseEntity<Result<Object>> processAdd(Model model , ExtendActBusiness businessEntity){
    	LoginUser user = getLoginUser();
        ValidatorUtils.validateEntity(businessEntity);
        businessEntity.setType(BusType.PROCESS.getType());
        int count = businessService.edit(businessEntity,user);
        if(count > 0){
        	return doSuccess("新增成功");
        }
        return doError("新增失败");
    }

    
    
    @RequestMapping("processEditView")
    @RequiresPermissions("act:bus:processEdit")
    public ModelAndView editView(ModelAndView modelAndView ,String menuId,String id){
    	LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
		ExtendActBusiness businessEntity = businessService.queryObject(id);
		Result<Organization> orgResult = ucenterApiService.queryOrg(businessEntity.getParentId());
		if(orgResult.codeSuccess()){
			modelAndView.addObject("parentName",orgResult.getData().getOrgName());
		}		
		
		modelAndView.addObject("record", businessEntity);
	  	modelAndView.setViewName("activiti/bus/process/edit");
       return  modelAndView;
    }
    
    /**
     * 保存或者更新
     * @param model
     * @param businessEntity
     * @return
     */
    @RequestMapping(value = "processEdit",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("act:bus:processEdit")
    public ResponseEntity<Result<Object>> processEdit(Model model , ExtendActBusiness businessEntity){
    	LoginUser user = getLoginUser();
        ValidatorUtils.validateEntity(businessEntity);
        businessEntity.setType(BusType.PROCESS.getType());
        Assert.notNull(businessEntity.getId(), "主键不能为空");
        int count = businessService.edit(businessEntity,user);
        if(count > 0){
        	return doSuccess("修改成功");
        }
        return doError("修改失败");
    }
    
  

    /**
     * 查看
     * @param id
     * @return
     */
    @RequestMapping(value = "processDetail",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("act:bus:processDetail")
    public ModelAndView  info(String menuId,String id, HttpServletRequest request,ModelAndView modelAndView){
    	LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
	  	ExtendActBusiness businessEntity = businessService.queryObject(id);
		modelAndView.addObject("record", businessEntity);
	  	modelAndView.setViewName("activiti/bus/process/detail");
       return  modelAndView;

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "processDelete",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("act:bus:processDelete")
    public ResponseEntity<Result<Object>> processdelete(String id, HttpServletRequest request){
        int count = businessService.delete(id);
        if(count>0){
        	return doSuccess("删除成功");
        }else {
        	return doError("删除失败");
        }
    }


    @SuppressWarnings("unchecked")
	@RequestMapping("callbackQuery")
    @RequiresPermissions("act:bus:callbackQuery")
    @ResponseBody
    public ResponseEntity<Map<String,Object>> callbackQuery(ModelAndView modelAndView , ExtendActBusiness businessEntity){
    	LoginUser user = getLoginUser();
    	businessEntity.setType(BusType.CALLBACK.getType());
        List<ExtendActBusiness> businessEntityList = businessService.queryListByBean(businessEntity,user);
        PageDto<ExtendActBusiness> page= this.createJQPage(request);
        page.setRecords(businessEntityList);
		return doSuccess(DataGridUtil.dataConvert(page));
    }

    
    
    @RequestMapping("callbackAddView")
    @RequiresPermissions("act:bus:callbackAdd")
    public ModelAndView callbackAddView(ModelAndView modelAndView ,String menuId,String parentId,String parentName){
    	LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
	 	modelAndView.addObject("parentId",parentId);
	  	modelAndView.addObject("parentName",parentName);
	  	modelAndView.setViewName("activiti/bus/callback/add");
       return  modelAndView;
    }
    
    /**
     * 保存或者更新
     * @param model
     * @param businessEntity
     * @return
     */
    @RequestMapping(value = "callbackAdd",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("act:bus:callbackAdd")
    public ResponseEntity<Result<Object>> callbackAdd(Model model , ExtendActBusiness businessEntity){
    	LoginUser user = getLoginUser();
        ValidatorUtils.validateEntity(businessEntity);
        businessEntity.setType(BusType.CALLBACK.getType());
        int count = businessService.edit(businessEntity,user);
        if(count > 0){
        	return doSuccess("新增成功");
        }
        return doError("新增失败");
    }

    
    
    @RequestMapping("callbackEditView")
    @RequiresPermissions("act:bus:callbackEdit")
    public ModelAndView calllbackEditView(ModelAndView modelAndView ,String menuId,String id){
    	LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
		ExtendActBusiness businessEntity = businessService.queryObject(id);
		ExtendActBusiness parent = businessService.queryObject(id);
		modelAndView.addObject("parentName",parent.getName());
		modelAndView.addObject("record", businessEntity);
	  	modelAndView.setViewName("activiti/bus/callback/edit");
       return  modelAndView;
    }
    
    /**
     * 保存或者更新
     * @param model
     * @param businessEntity
     * @return
     */
    @RequestMapping(value = "callbackEdit",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("act:bus:callbackEdit")
    public ResponseEntity<Result<Object>> callbackEdit(Model model , ExtendActBusiness businessEntity){
    	LoginUser user = getLoginUser();
        ValidatorUtils.validateEntity(businessEntity);
        businessEntity.setType(BusType.CALLBACK.getType());
        Assert.notNull(businessEntity.getId(), "主键不能为空");
        int count = businessService.edit(businessEntity,user);
        if(count > 0){
        	return doSuccess("修改成功");
        }
        return doError("修改失败");
    }
    
  

    /**
     * 查看
     * @param id
     * @return
     */
    @RequestMapping(value = "callbackDetail",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("act:bus:callbackDetail")
    public ModelAndView  callbackDetail(String menuId,String id, HttpServletRequest request,ModelAndView modelAndView){
    	LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
	  	ExtendActBusiness businessEntity = businessService.queryObject(id);
		modelAndView.addObject("record", businessEntity);
	  	modelAndView.setViewName("activiti/bus/callback/detail");
       return  modelAndView;

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "callbackDelete",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("act:bus:callbackDelete")
    public ResponseEntity<Result<Object>> callbackDelete(String id, HttpServletRequest request){
        int count = businessService.delete(id);
        if(count>0){
        	return doSuccess("删除成功");
        }else {
        	return doError("删除失败");
        }
    }





}
